<html>
<head>
<script type="text/javascript">

function each(coll, act){
	for(var i=0; i<coll.length; i++){var el = coll[i];
		act(el, i);
	}
}

function merge(c1, c2, andLogic, idF){
	if(!idF)
		idF = function(x){return x;}
	var h = {};
	function ins(x){
		var idx = idF(x);
		if(h[idx]==null)
			h[idx] = {itm:x, count:0};
		h[idx].count++;
	}
	each(c1, ins);
	each(c2, ins);
	var res = [];
	for(var k in h){
		if(andLogic){
			if(h[k].count==2)
				res.push(h[k].itm);
		}
		else{
			if(h[k])
				res.push(h[k].itm);
		}
	}
	return res;
}

function write(str){
	document.getElementById("out").innerHTML+=str+"<br>";
}

function init(){
	var c1 = [1,2,3];
	var c2 = [1,3,4];
	
	var c = merge(c1, c2, true);
	if(c.length!=2)
		write("error 1.1");
	if(c[0]!=1)
		write("error 1.2");
	if(c[1]!=3)
		write("error 1.3");
	
	
	c = merge(c1, c2);
	if(c.length!=4)
		write("error 2.1");
	if(c[0]!=1)
		write("error 2.2");
	if(c[1]!=2)
		write("error 2.3");
	if(c[2]!=3)
		write("error 2.4: "+c[2]);
	if(c[3]!=4)
		write("error 2.5: "+c[3]);
	
	
	c1 = [{x:1, y:1},{x:1, y:2},{x:1, y:3}];
	c2 = [{x:1, y:1},{x:2, y:2}];
	
	c = merge(c1, c2, true, function(x){return x.x+"."+x.y;});
	if(c.length!=1)
		write("error 3.1: "+c.length);
	if(c[0].x!=1)
		write("error 3.2: "+c[0].x);
	if(c[0].y!=1)
		write("error 3.3: "+c[0].y);
	
	
	write("done");
}
</script>
</head>
<body onload="init()">
<div id="out"></div>
</body>
</html>